package com.ita.util;

import java.math.BigInteger;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;

import org.apache.camel.model.dataformat.JibxDataFormat;

public class RSAUtil {

	private static RSAUtil instance = new RSAUtil();

	private static String publicKey;
	private static String privateKey;
	static {

		try {
			Map<String, Object> keyMap = initKey();
			publicKey = getPublicKey(keyMap);
			privateKey = getPrivateKey(keyMap);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private RSAUtil() {

	}

	public static RSAUtil getInstance() {
		return instance;
	}

	static {

	}

	/**
	 * 用私钥对信息生成数字签名
	 * 
	 * @param data
	 *            加密数据
	 * @param privateKey
	 *            私钥
	 * 
	 * @return
	 * @throws Exception
	 */
	private String sign(byte[] data, String privateKey) throws Exception {

		byte[] keyBytes = Base64Util.base64ToByte(privateKey);

		PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);

		KeyFactory keyFactory = KeyFactory.getInstance(Constant.RSA_ALGORITHM);

		PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
		Signature signature = Signature.getInstance(Constant.SIGNATURE_ALGORITHM);
		signature.initSign(priKey);
		signature.update(data);

		return Base64Util.byteToBase64(signature.sign());
	}

	private boolean verify(byte[] data, String publicKey, String sign) throws Exception {

		byte[] keyBytes = Base64Util.base64ToByte(publicKey);

		X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);

		KeyFactory keyFactory = KeyFactory.getInstance(Constant.RSA_ALGORITHM);

		PublicKey pubKey = keyFactory.generatePublic(keySpec);

		Signature signature = Signature.getInstance(Constant.SIGNATURE_ALGORITHM);
		signature.initVerify(pubKey);
		signature.update(data);

		return signature.verify(Base64Util.base64ToByte(sign));
	}

	/**
	 * 解密<br>
	 * 用私钥解密
	 * 
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	private byte[] decryptByPrivateKey(byte[] data, String key) throws Exception {

		byte[] keyBytes = Base64Util.base64ToByte(key);

		PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(Constant.RSA_ALGORITHM);
		Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);

		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.DECRYPT_MODE, privateKey);

		return cipher.doFinal(data);
	}

	/**
	 * 解密<br>
	 * 用公钥解密
	 * 
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	private byte[] decryptByPublicKey(byte[] data, String key) throws Exception {

		byte[] keyBytes = Base64Util.base64ToByte(key);

		X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(Constant.RSA_ALGORITHM);
		Key publicKey = keyFactory.generatePublic(x509KeySpec);

		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.DECRYPT_MODE, publicKey);

		return cipher.doFinal(data);
	}

	/**
	 * 加密<br>
	 * 用公钥加密
	 * 
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	private byte[] encryptByPublicKey(byte[] data, String key) throws Exception {

		byte[] keyBytes = Base64Util.base64ToByte(key);

		X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(Constant.RSA_ALGORITHM);
		Key publicKey = keyFactory.generatePublic(x509KeySpec);

		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.ENCRYPT_MODE, publicKey);

		return cipher.doFinal(data);
	}

	/**
	 * 加密<br>
	 * 用私钥加密
	 * 
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	private byte[] encryptByPrivateKey(byte[] data, String key) throws Exception {

		byte[] keyBytes = Base64Util.base64ToByte(key);

		PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(Constant.RSA_ALGORITHM);
		Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);

		// 对数据加密
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.ENCRYPT_MODE, privateKey);

		return cipher.doFinal(data);
	}

	/**
	 * 取得私钥
	 * 
	 * @param keyMap
	 * @return
	 * @throws Exception
	 */
	private static String getPrivateKey(Map<String, Object> keyMap) throws Exception {
		Key key = (Key) keyMap.get(Constant.PRIVATE_KEY);

		return Base64Util.byteToBase64(key.getEncoded());
	}

	/**
	 * 取得公钥
	 * 
	 * @param keyMap
	 * @return
	 * @throws Exception
	 */
	private static String getPublicKey(Map<String, Object> keyMap) throws Exception {
		Key key = (Key) keyMap.get(Constant.PUBLIC_KEY);

		return Base64Util.byteToBase64(key.getEncoded());
	}

	/**
	 * 初始化密钥
	 * 
	 * @return
	 * @throws Exception
	 */
	private static Map<String, Object> initKey() throws Exception {
		KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(Constant.RSA_ALGORITHM);
		keyPairGen.initialize(1024);

		KeyPair keyPair = keyPairGen.generateKeyPair();

		// 公钥
		RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();

		// 私钥
		RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

		Map<String, Object> keyMap = new HashMap<String, Object>(2);

		keyMap.put(Constant.PUBLIC_KEY, publicKey);
		keyMap.put(Constant.PRIVATE_KEY, privateKey);
		return keyMap;
	}

	public String encryt(String inputStr) {
		try {
			byte[] data = inputStr.getBytes("UTF-8");
			byte[] encodedData = encryptByPublicKey(data, publicKey);
			return Base64Util.byteToBase64(encodedData);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public String decryt(String inputStr) {
		try {
			byte[] encodedData = Base64Util.base64ToByte(inputStr);
			byte[] decodedData = decryptByPrivateKey(encodedData, privateKey);
			String outputStr = new String(decodedData, "UTF-8");
			return outputStr;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

}
